﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Casemeet.Elites;
using VK.Comm;
using VK.Casemeet.Employees;

namespace VK.Casemeet.Web.Admin.Elites
{
    public partial class EliteEdit : System.Web.UI.Page
    {
        public int eliteID = 0;
        public string content = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="精英",Url="Index.aspx"},
                new MapPath{ Text="编辑精英",Url="#"}
            };
            
            eliteID = Util.GetQueryInt("EliteID");

            if (!IsPostBack)
            {
                lbEditor.Text = EmployeeService.GetSessionEmployee().Account;
                LoadData();
            }
        }

        internal void LoadData()
        {
            if (eliteID != 0)
            {
                EliteEntity elite = EliteBLL.GetElite(eliteID);
                txtTitle.Text = elite.Title;
                content = elite.Content;
                chkIsEnable.Checked = elite.IsEnable;
                txtCreated.Text = Util.FormatDateTime(elite.Created, DateTimeStyle.Null);
                txtHits.Text = elite.Hits.ToString();
                newsPhoto1.defaultImgUrl = elite.ImgUrl;
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            EliteEntity elite = LoadEntity();
            elite.EliteID = eliteID;

            if (EliteBLL.UpdateElite(elite) > 0)
            {
                Message.Msg(this.Page, CS.MsgEditSuccess, MessageType.Success);
            }
            else
            {
                Message.Msg(this.Page, CS.MsgEditFailure, MessageType.Failure);
            }
            LoadData();
        }

        protected EliteEntity LoadEntity()
        {
            EliteEntity elite = EliteBLL.GetElite(eliteID);
            elite.Title = txtTitle.Text;
            elite.Content = Server.HtmlEncode(Util.GetFormString("txtContent"));
            elite.IsEnable = chkIsEnable.Checked;
            elite.Updated = Util.ConverDateTime(txtCreated.Text);
            elite.Hits = int.Parse(txtHits.Text);
            elite.Editor = lbEditor.Text;
            elite.ImgUrl = newsPhoto1.ValueUrl;

            return elite;
        }
    }
}